<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Description of city_model
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Admin_city_model extends CI_Model {

    //put your code here

    function __construct() {
        parent::__construct();
        log_message('debug', 'Model Initialized');
    }

    //--Subha
    function top_city($param) {
        $this->db->where('topdisplay', 'yes');
        $this->db->limit($param);
        $query = $this->db->get('city');
        $result = $query->result();
        return $result;
    }

    //--Subha
    function bottom_city($param) {
        $this->db->where('bottomdisplay', 'yes');
        $this->db->limit($param);
        $query = $this->db->get('city');
        $result = $query->result();
        return $result;
    }

    function site_navigation() {
        $this->db->where('status', '51');
        $query = $this->db->get('cms');
        $result = $query->result();
        return $result;
    }

    function show_cms_content($id=NULL) {
        $this->db->where('id', $id);
        $query = $this->db->get('cms');
        $result = $query->result();
        return $result;
    }

    function get_city_id($city_name=NULL) {
        if ($city_name) {
            $this->db->where('city', trim($city_name));
            $query = $this->db->get('city');
            $row = $query->row();

            if ($query->num_rows == 1)
                return $row->id;
        }
    }

    function get_city_name($city_id=NULL) {
        if ($city_id) {
            $this->db->where('id', $city_id);
            $query = $this->db->get('city');
            $row = $query->row();

            if ($query->num_rows == 1)
                return $row->city;
        }
    }

    function get_state_name_by_city($city_id=NULL) {
        if ($city_id) {
            $sql = "select stateid from city where id=$city_id";
            $query = $this->db->query($sql);
            $result = $query->result();
            if ($query->num_rows() > 0) {
                $key = '';
                foreach ($result as $keyword_id):
                    $key .= $keyword_id->stateid . ',';
                endforeach;
                $length = strlen($key);
                $key = substr_replace($key, '', $length - 1, $length);
            }
            $sql = "SELECT statename FROM `state` WHERE id in($key)";
            $query = $this->db->query($sql);
            $result = $query->row();
            return $result->statename;
        }
    }

    function get_country_name_by_city($city_id=NULL) {
        if ($city_id) {
            if ($city_id) {
                $sql = "select stateid from city where id=$city_id";
                $query = $this->db->query($sql);
                $result = $query->result();
                if ($query->num_rows() > 0) {
                    $key = '';
                    foreach ($result as $keyword_id):
                        $key .= $keyword_id->stateid . ',';
                    endforeach;
                    $length = strlen($key);
                    $key = substr_replace($key, '', $length - 1, $length);
                }
                $sql = "select country from country where id in(SELECT countryid FROM `state` WHERE id in($key))";
                $query = $this->db->query($sql);
                $result = $query->row();
                return $result->country;
            }
        }
    }

}

?>
